package com.itheima.day3.stream;

import java.util.*;
import java.util.stream.Collectors;

// Collector 收集器
public class C12CollectorTest {
    record Hero(String name, int strength) {
    }

    public static void main(String[] args) {
        List<String> collection = List.of("令狐冲", "风清扬", "独孤求败", "方证",
                "东方不败", "冲虚", "向问天", "任我行", "不戒");
        /*
            1) 收集到 List
         */
//        List<String> result11 = collection.stream().collect(ArrayList::new, ArrayList::add, (a, b) -> { });
        List<String> result1 = collection.stream().collect(Collectors.toList());

        /*
            2) 收集到 Set
         */
//        Set<String> result22 = collection.stream().collect(LinkedHashSet::new, Set::add, (a, b) -> { });
        Set<String> result2 = collection.stream().collect(Collectors.toSet());

        /*
            3)收集到 StringBuilder
         */
//        StringBuilder result33 = collection.stream().collect(StringBuilder::new, StringBuilder::append, (a,b)->{});
        String result3 = collection.stream().collect(Collectors.joining());

        /*
            4)收集到 StringJoiner
         */
//        StringJoiner result44 = collection.stream().collect(()->new StringJoiner(","), StringJoiner::add, (a, b)->{});
        String result4 = collection.stream().collect(Collectors.joining(","));

        /*
            3)收集到 Map
         */
//        Map<String, Integer> result55 = collection.stream().collect(HashMap::new, (map, x)->map.put(x, 1), (a, b) -> { });
        Map<String, Integer> result5 = collection.stream().collect(Collectors.toMap(x -> x, x -> 1));

        /*
            Map
            3: new ArrayList(["令狐冲","风清扬","向问天","任我行"])
            4: new ArrayList(["独孤求败","东方不败"])
            2: new ArrayList(["方证","冲虚","不戒"])

            Collectors.toList() 下游收集器
         */

        Map<Integer, List<String>> result6 = collection.stream().collect(Collectors.groupingBy(x -> x.length(), Collectors.toList()));

        Map<Integer, String> result7 = collection.stream().collect(Collectors.groupingBy(x -> x.length(), Collectors.joining(",")));
    }
}
